package com.sport.apis.aop;

import com.sport.apis.annotation.SportStatus;
import com.sport.apis.exception.CustomException;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.user.bos.AccountUserBo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class SportStatusAop {

    @Pointcut(value = "@within(com.sport.apis.annotation.SportStatus) || @annotation(com.sport.apis.annotation.SportStatus)")
    public void checkSportStatus() {}

    @Around("@within(sportStatus)")
    public Object checkOperation(ProceedingJoinPoint proceedingJoinPoint, SportStatus sportStatus) throws Throwable {
        SportStatus annotation = proceedingJoinPoint.getTarget().getClass().getAnnotation(SportStatus.class);
        shareModel( annotation.sportTargetStatus().getIndexStatus(), AccountUserBo.getUser().getSportStatus());
        return proceedingJoinPoint.proceed();
    }

    @Before("@annotation(sportStatus)")
    public void checkOperationMethod(SportStatus sportStatus) {
        shareModel(sportStatus.sportTargetStatus().getIndexStatus(), AccountUserBo.getUser().getSportStatus());
    }

    private void shareModel(Integer target, Integer current) {
        if (target > current)
            throw new CustomException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
    }
}
